#include<bits/stdc++.h>
using namespace std;
#define rd(i,n) for(int i=0;i<n;i++)
#define rp(i,n) for(int i=1;i<=n;i++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=b;i>=a;i--)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef unsigned long long ull;
const int N=200005;
ll n,a[N],dp[N],s[N];
int mxc[1000005],mx;
inline ll check(int x,int y){
    //check the value of x at time y
    return dp[x]+s[y]-s[x+1];
}
inline void Add(int x){
    if(mxc[a[x]]==0)mxc[a[x]]=x;
    else if(check(x,x+1)>check(mxc[a[x]],x+1)){
        mxc[a[x]]=x;
    }
    if(mx==0)mx=x;
    else if(check(x,x+1)>check(mx,x+1)){
        mx=x;
    }
}
inline void solve(){
    cin>>n;
    rp(i,n)cin>>a[i];
    rp(i,n)mxc[a[i]]=0;
    mx=0,dp[0]=0,s[0]=0,a[n+1]=0;
    rp(i,n){
        s[i]=s[i-1]+(a[i-1]==a[i])*a[i];
        Add(i-1);
        dp[i]=check(mx,i);
        if(mxc[a[i+1]])dp[i]=max(dp[i],check(mxc[a[i+1]],i)+a[i+1]);
    }
    cout<<dp[n]<<'\n';
}
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    rd(_,t)solve();
    return 0;
}
//g++ color.cpp -o color -O2 -std=c++14 -static
